﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using EAT.Common;
using System.Web.UI;
using System.Configuration;
using Microsoft.SharePoint;

namespace ECTSUserMgmtExtension
{
    public class UserManagerUserEditor : WebPart
    {
        #region InitializeUserToEdit
        private string m_userAccountToEdit;

        [ConnectionConsumer("User Editor Consumer", "UserManagerUserEditorConsumer")]
        public void InitializeUserToEdit(IUserManagerUserEditor userManager)
        {
            m_userAccountToEdit = userManager.UserAccountToEdit;
        }
        #endregion

        #region UserManager
        private cUserManager _cUserManager;
        protected cUserManager UserManager
        {
            get
            {
                if (_cUserManager == null)
                    _cUserManager = new cUserManager(ConfigurationManager.ConnectionStrings["ADAMConnectionString"].ConnectionString);

                return _cUserManager;
            }
        }
        #endregion

        private TextBox m_firstName;
        private TextBox m_lastName;
        private TextBox m_phoneNumber;
        private TextBox m_service;
        private TextBox m_notes;
        private TextBox m_accountToEdit;

        protected override void CreateChildControls()
        {
            Table table = new Table();
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            td.Text = "First Name";
            tr.Cells.Add(td);

            td = new TableCell();
            m_firstName = new TextBox();
            td.Controls.Add(m_firstName);
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            tr = new TableRow();
            td = new TableCell();
            td.Text = "Last Name";
            tr.Cells.Add(td);

            td = new TableCell();
            m_lastName = new TextBox();
            td.Controls.Add(m_lastName);
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            tr = new TableRow();
            td = new TableCell();
            td.Text = "Phone Number";
            tr.Cells.Add(td);

            td = new TableCell();
            m_phoneNumber = new TextBox();
            td.Controls.Add(m_phoneNumber);
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            tr = new TableRow();
            td = new TableCell();
            td.Text = "Service";
            tr.Cells.Add(td);

            td = new TableCell();
            m_service = new TextBox();
            td.Controls.Add(m_service);
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            tr = new TableRow();
            td = new TableCell();
            td.Text = "Notes";
            td.ColumnSpan = 2;
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            tr = new TableRow();
            td = new TableCell();
            m_notes = new TextBox();
            m_notes.TextMode = TextBoxMode.MultiLine;
            td.Controls.Add(m_notes);
            td.ColumnSpan = 2;
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            tr = new TableRow();
            td = new TableCell();
            td.HorizontalAlign = HorizontalAlign.Center;
            Button update = new Button();
            update.Text = "Update";
            update.Click += new EventHandler(UpdateUserInfo);
            td.Controls.Add(update);
            tr.Cells.Add(td);

            table.Rows.Add(tr);

            Controls.Add(table);

            m_accountToEdit = new TextBox();
            m_accountToEdit.Visible = false;
            Controls.Add(m_accountToEdit);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!string.IsNullOrEmpty(m_userAccountToEdit))
            {
                using (DirectoryEntry user = UserManager.FindUser(m_userAccountToEdit))
                {
                    m_accountToEdit.Text = m_userAccountToEdit;
                    try { m_firstName.Text = user.Properties["givenName"].Value.ToString(); }
                    catch { }
                    try { m_lastName.Text = user.Properties["sn"].Value.ToString(); }
                    catch { }
                    try { m_notes.Text = user.Properties["comment"].Value.ToString(); }
                    catch { }
                    try { m_phoneNumber.Text = user.Properties["telephoneNumber"].Value.ToString(); }
                    catch { }
                    try { m_service.Text = user.Properties["company"].Value.ToString(); }
                    catch { }
                }
            }

            base.OnPreRender(e);
        }

        void UpdateUserInfo(object sender, EventArgs args)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                string userId = m_accountToEdit.Text;
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(
                        delegate()
                        {
                            using (DirectoryEntry user = UserManager.FindUser(userId))
                            {
                                if (!string.IsNullOrEmpty(m_firstName.Text))
                                    user.Properties["givenName"].Value = m_firstName.Text;
                                if (!string.IsNullOrEmpty(m_lastName.Text))
                                    user.Properties["sn"].Value = m_lastName.Text;
                                if (!string.IsNullOrEmpty(m_notes.Text))
                                    user.Properties["comment"].Value = m_notes.Text;
                                if (!string.IsNullOrEmpty(m_phoneNumber.Text))
                                    user.Properties["telephoneNumber"].Value = m_phoneNumber.Text;
                                if (!string.IsNullOrEmpty(m_service.Text))
                                    user.Properties["company"].Value = m_service.Text;

                                user.CommitChanges();
                                Controls.Add(new LiteralControl("<p>User Information Updated</p>"));

                                m_firstName.Text = "";
                                m_lastName.Text = "";
                                m_notes.Text = "";
                                m_phoneNumber.Text = "";
                                m_service.Text = "";
                            }
                        }
                    );
                }
                catch
                {
                    Controls.Add(new LiteralControl("<p>Failed to Update User Information</p>"));
                }
            }
        }
    }
}
